﻿using System;
using HslCommunication.BasicFramework;

namespace HslCommunication.Serial
{
	/// <summary>
	/// 用于LRC验证的类，提供了标准的验证方法<br />
	/// The class used for LRC verification provides a standard verification method
	/// </summary>
	// Token: 0x0200002B RID: 43
	public class SoftLRC
	{
		/// <summary>
		/// 获取对应的数据的LRC校验码<br />
		/// Class for LRC validation that provides a standard validation method
		/// </summary>
		/// <param name="value">需要校验的数据，不包含LRC字节</param>
		/// <returns>返回带LRC校验码的字节数组，可用于串口发送</returns>
		// Token: 0x06000320 RID: 800 RVA: 0x0000C484 File Offset: 0x0000A684
		public static byte[] LRC(byte[] value)
		{
			bool flag = value == null;
			byte[] result;
			if (flag)
			{
				result = null;
			}
			else
			{
				int num = 0;
				for (int i = 0; i < value.Length; i++)
				{
					num += (int)value[i];
				}
				num %= 256;
				num = 256 - num;
				byte[] array = new byte[]
				{
					(byte)num
				};
				result = SoftBasic.SpliceArray<byte>(new byte[][]
				{
					value,
					array
				});
			}
			return result;
		}

		/// <summary>
		/// 检查数据是否符合LRC的验证<br />
		/// Check data for compliance with LRC validation
		/// </summary>
		/// <param name="value">等待校验的数据，是否正确</param>
		/// <returns>是否校验成功</returns>
		// Token: 0x06000321 RID: 801 RVA: 0x0000C4F4 File Offset: 0x0000A6F4
		public static bool CheckLRC(byte[] value)
		{
			bool flag = value == null;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				int num = value.Length;
				byte[] array = new byte[num - 1];
				Array.Copy(value, 0, array, 0, array.Length);
				byte[] array2 = SoftLRC.LRC(array);
				bool flag2 = array2[num - 1] == value[num - 1];
				result = flag2;
			}
			return result;
		}
	}
}
